// pages/goods_list/index.js
import { request } from "../../request/index"
import { request1 } from "../../request1/index"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs:[
      {
        id:0,
        value:"综合",
        isActive:true
      },
      {
        id:1,
        value:"销售",
        isActive:false
      },
      {
        id:2,
        value:"价格",
        isActive:false
      },

    ],
    goodslist:[]
  },
  Query:{
    query:"",
    cid:"",
    pagenum:1,
    pagesize:6
  },
  totalpages:1,
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.Query.cid = options.cid,
    console.log(options)
    // this.getgoodsList("/goods/search"),
   
    
 
    this.getcatelist1()
    // wx.request({

      
    // })
  },
  getcatelist1(){
    request1({url:'https://api-hmugo-web.itheima.net/api/public/v1/goods/search?cid='+this.Query.cid+'&pagenum='+this.Query.pagenum+'&pagesize='+this.Query.pagesize}).then(res=>{
      console.log(res)
      const total = res.data.message.total
      this.totalpages = Math.ceil(total/this.Query.pagesize)
      this.setData({
        goodslist:[...this.data.goodslist,...res.data.message.goods]
      })
    })
    // request()
    // url: 'https://api-hmugo-web.itheima.net/api/public/v1/goods/search?cid='+this.Query.cid+'&pagenum='+this.pagesize,
    // success:(res)=>{
    //   console.log(res)
    //   const total = res.data.message.total
    //   this.totalpages = Math.ceil(total / this.Query.pagesize)
    //   this.setData({  

    //     goodslist:[...this.data.goodslist,...res.data.message.goods]
    //   })
    // }
  },
  tabsitemchange(e){
    const {index} = e.detail;
    let {tabs} = this.data;
    tabs.forEach((v,id) => id===index?v.isActive=true:v.isActive=false);
    this.setData({
      tabs
    })
    
  },
  onReachBottom(){
    // console.log('dididid')
    if(this.Query.pagenum>=this.totalpages){
      console.log("NONONO")
    }else{
      this.Query.pagenum++;
      this.getcatelist1();
      
    }
  },
  // getgoodsList(url){
  //   request({ url:url})
  //   .then( result =>{
  //     console.log(result)
  //     this.setData({
  //       goodslist:result.data.message.goods
  //     })
  //   })
  // },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  /**
   * 页面上拉触底事件的处理函数
   */

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})